// pages/draw/print.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    name:'',

    addresss:'',
    longitude:'',
    latitude:'',

    phone:'',

    index: 0,
    sid: '',
    sidname: '',
    array: [
      // { title: '地点一', id: 1 },
      // { title: '地点二', id: 2 },
      // { title: '地点三', id: 3 },
    ],

    nums:'',

    index2: 0,
    dytype: '',
    dytypename: '',
    array2: [
      // { title: '12x12', id: 1 },
      // { title: '15x15', id: 2 },
      // { title: '18x18', id: 3 },
    ],

    type: 1,//1自取   0 配送
    show:false,
    money:'',
    address:''
  },
  getinput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  getinput2(e) {
    this.setData({
      nums: e.detail.value
    })
  },
  chooseLocation() {
    let that = this;
    wx.chooseLocation({
      success: function(res) {
        // console.log(res.longitude);
        // console.log(res.latitude);
        // console.log(res.address);
        that.setData({
          longitude:res.longitude,
          latitude:res.latitude,
          address:res.address
        })
      },
      fail: function(err) {
        console.log(err)
        wx.showToast({
          title: err,
          icon:'none',
          duration:2000
        })
      }
    })
  },
  // 获取手机号
  getPhoneNumber(e) {
    // console.log(e.detail.code)  // 动态令牌
    // console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
    // console.log(e.detail.errno)  // 错误码（失败时返回）
    app.util.request({
      url: '/user/getUserPhone',
      data: {
        code:e.detail.code
      }
    }).then(res => {
      this.setData({
        phone:res.data.phoneNumber
      })
    })
  },
  // 获取收货地址
  // aaaaaaa() {
  //   wx.getSetting({
  //     success(res) {
  //       console.log("vres.authSetting['scope.address']：", res.authSetting['scope.address'])
  //       if (res.authSetting['scope.address']) {
  //         console.log("111")
  //         wx.chooseAddress({
  //           success(res) {
  //             console.log(res.userName)
  //             console.log(res.postalCode)
  //             console.log(res.provinceName)
  //             console.log(res.cityName)
  //             console.log(res.countyName)
  //             console.log(res.detailInfo)
  //             console.log(res.nationalCode)
  //             console.log(res.telNumber)
  //           }
  //         })
  //       } else {
  //         if (res.authSetting['scope.address'] == false) {
  //           console.log("222")
  //           wx.openSetting({
  //             success(res) {
  //               console.log(res.authSetting)
  //             }
  //           })
  //         } else {
  //           console.log("eee")
  //           wx.chooseAddress({
  //             success(res) {
  //               console.log(res.userName)
  //               console.log(res.postalCode)
  //               console.log(res.provinceName)
  //               console.log(res.cityName)
  //               console.log(res.countyName)
  //               console.log(res.detailInfo)
  //               console.log(res.nationalCode)
  //               console.log(res.telNumber)
  //             }
  //           })
  //         }
  //       }
  //     }
  //   })
  // },
  // 打印点
  bindPickerChange(e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e)
    if (that.data.array.length == 0) {
      return
    }
    that.setData({
      index: e.detail.value,
      sidname: that.data.array[e.detail.value].title,
      sid: that.data.array[e.detail.value].id,
      address:that.data.array[e.detail.value].address
    })
  },
  // 尺寸
  bindPickerChange2(e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e)
    if (that.data.array2.length == 0) {
      return
    }
    that.setData({
      index: e.detail.value,
      dytypename: that.data.array2[e.detail.value].title,
      dytype: that.data.array2[e.detail.value].id,
    })
  },
  // 配送
  tabtype() {
    let type = this.data.type;
    this.setData({
      type: type == 1 ? type = 0 : type = 1
    })
  },
  async toShow(){
    if(!this.data.name){
      wx.showToast({
        title: '请输入收件人姓名',
        icon:'none'
      })
      return
    } 
    if(!this.data.address || !this.data.longitude || !this.data.latitude){
      wx.showToast({
        title: '请选择地址',
        icon:'none'
      })
      return
    }
    if(!this.data.phone){
      wx.showToast({
        title: '请获取联系方式',
        icon:'none'
      })
      return
    } 
    if(!this.data.sid){
      wx.showToast({
        title: '请选择打印点',
        icon:'none'
      })
      return
    } 
    if(!this.data.nums){
      wx.showToast({
        title: '请输入张数',
        icon:'none'
      })
      return
    } 
    if(!this.data.dytype){
      wx.showToast({
        title: '请选择尺寸',
        icon:'none'
      })
      return
    } 
    let params = {
      zid:this.data.id,
      name:this.data.name,
      address:this.data.address,
      lng:this.data.longitude,
      lat:this.data.latitude,
      phone:this.data.phone,
      sid:this.data.sid,
      num:this.data.nums,
      dytype:this.data.dytype,
      type:this.data.type
    }
    await app.util.request({
      url: '/user/dyorderprice',
      data: params
    }).then(res => {
      this.setData({
        money:res.data.price,
        show:true
      })
    })
  },
  toClose(){
    this.setData({
      show:false
    })
  },
  // 提交
  async submit() {
    if(!this.data.name){
      wx.showToast({
        title: '请输入收件人姓名',
        icon:'none'
      })
      return
    } 
    if(!this.data.address || !this.data.longitude || !this.data.latitude){
      wx.showToast({
        title: '请选择地址',
        icon:'none'
      })
      return
    }
    if(!this.data.phone){
      wx.showToast({
        title: '请获取联系方式',
        icon:'none'
      })
      return
    } 
    if(!this.data.sid){
      wx.showToast({
        title: '请选择打印点',
        icon:'none'
      })
      return
    } 
    if(!this.data.nums){
      wx.showToast({
        title: '请输入张数',
        icon:'none'
      })
      return
    } 
    if(!this.data.dytype){
      wx.showToast({
        title: '请选择尺寸',
        icon:'none'
      })
      return
    } 
    let params = {
      zid:this.data.id,
      name:this.data.name,
      address:this.data.address,
      lng:this.data.longitude,
      lat:this.data.latitude,
      phone:this.data.phone,
      sid:this.data.sid,
      num:this.data.nums,
      dytype:this.data.dytype,
      type:this.data.type
    }
    await app.util.request({
      url: '/user/dyorder',
      data: params
    }).then(res => {
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: 'MD5',
        paySign: res.data.paySign,
        success: function (res) {
          app.util.message('支付成功', 'error', function () {
            wx.switchTab({
              url: '/pages/user/index',
            })
          })
        },
        fail: function (res) {
          app.util.message('支付失败，请重试', 'error');
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:options.id
    })
    app.util.request({
      url: '/user/serverlist',
      data:{}
    }).then(res => {
      this.setData({
        array:res.message
      })
    })
    app.util.request({
      url: '/user/dytype',
      data:{}
    }).then(res => {
      this.setData({
        array2:res.message
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})